package com.hnhy.hnhyscms_ws.controller;

import com.hnhy.hnhyscms_ws.entity.Spstorage;
import com.hnhy.hnhyscms_ws.entity.Suppliers;
import com.hnhy.hnhyscms_ws.service.ISpstorageService;
import com.hnhy.hnhyscms_ws.service.ISuppliersSevice;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping(value = "/spstorage")
public class SpstorageController {
    @Autowired
    private ISpstorageService iSpstorageService;
    @Autowired
    private ISuppliersSevice iSuppliersSevice;
    private static Logger logger = LoggerFactory.getLogger(SpstorageController.class);
    @RequestMapping(value = "/getAllProducts",method= RequestMethod.GET)
    public List<Spstorage> getAllProducts(){ return iSpstorageService.getAllProducts(); }
    @RequestMapping(value = "/getByProductNo",method = RequestMethod.POST)
    public Spstorage getByProductNo(@RequestParam String productNo){ return  iSpstorageService.getByProductNo(productNo); }
    @RequestMapping(value = "/addProducts",method = RequestMethod.POST)
    public void addProducts(
            @RequestParam String productName,
            @RequestParam String supplierNo,
            @RequestParam String band,
            @RequestParam String spec,
            @RequestParam String unit,
            @RequestParam Float price,
            @RequestParam Float amount)
    {
        Suppliers suppliers=iSuppliersSevice.getBySupplierNo(supplierNo);
        Spstorage spstorage=new Spstorage();
        spstorage.setProductName(productName);
        spstorage.setSupplierNo(supplierNo);
        spstorage.setSupplierName(suppliers.getSupplierName());
        spstorage.setBand(band);
        spstorage.setSpec(spec);
        spstorage.setUnit(unit);
        spstorage.setPrice(price);
        spstorage.setAmount(amount);
        spstorage.setProductNo("smgy"+"0000"+(iSpstorageService.findMaxId().getId()+1));
        iSpstorageService.addProducts(spstorage);
        return;
    }
    @RequestMapping(value = "/importpic",method = RequestMethod.POST)
    public void importPic(@RequestParam MultipartFile file, @RequestParam String  productName) throws IOException {
        String Filepath="F://____Files/_CCT/_hy/hnhyscms/static/spstorage/";//注意更改文件存储位置
        File filepath = new File(Filepath);
        if (!filepath.exists()) {
            filepath.mkdirs();
        }
        File file1 = new File(Filepath+productName+".jpg");
        file.transferTo(file1);
        if(filepath.exists()){
            logger.info("上传成功");
        }else {
            filepath.mkdirs();
            logger.info("上传失败{}",filepath);
        }
    }
    @RequestMapping(value = "/changeProduct",method = RequestMethod.POST)
    public synchronized void changeProduct(
            @RequestParam String productNo,
            @RequestParam String productName,
            @RequestParam String spec,
            @RequestParam String unit,
            @RequestParam Float price)
    {
        Spstorage spstorage=new Spstorage();
        spstorage.setProductName(productName);
        spstorage.setSpec(spec);
        spstorage.setUnit(unit);
        spstorage.setPrice(price);
        spstorage.setProductNo(productNo);
        iSpstorageService.changeByProductNo(spstorage);
        return;
    }
    @RequestMapping(value = "/deleteByProductNo",method = RequestMethod.POST)
    private void deleteByProductNo(@RequestParam String productNo){
        iSpstorageService.deleteByProductNo(productNo);
    }
}
